﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI;

namespace MVC.Web.UI.Accordion
{
     public class WebAccordion : MvcControl
    {
        #region Fields
    
       // private Dictionary<TabEvents, string> _tabEventHandlers; 
        private WebAccordionCollection<NavigationItem> _webAccordionItems;
        private HtmlHelper _htmlHelper;
                 
        #endregion

        #region Constructors

        public WebAccordion(HtmlHelper htmlHelper)
            : base("div", htmlHelper.ViewContext)
        {
        } 
        #endregion

        #region Properties
        
        //public Dictionary<TabEvents, string> TabEventHandlers
        //{
        //    get {
        //            if (this._tabEventHandlers == null)
        //                return this._tabEventHandlers = new Dictionary<TabEvents, string>();
        //            return this._tabEventHandlers; 
                    
        //        }
        //} 

        public WebAccordionCollection<NavigationItem> AccordionItems
        {
            get
            {
                if (this._webAccordionItems == null)
                    return this._webAccordionItems = new WebAccordionCollection<NavigationItem>();
                return _webAccordionItems;
            }
            set { _webAccordionItems = value; }
        }

        public HtmlHelper HtmlHelper
        {
            get { return _htmlHelper; }
            set { _htmlHelper = value; }
        }
  
        public Action Items { get; set; }
        
         /// <summary>
        ///The name of the control based on which it is identified on the client 
        /// </summary>
        public string  Name { get; set; }

        /// <summary>
        /// Selector for the active element. Set to false to display none at start. Needs
        /// </summary>
        public int Activate { get; set; }

        
        /// <summary>
        /// Choose your favorite animation, 
        /// or disable them (set to false). In addition to the default, 'bounceslide' and all defined easing methods are supported ('bounceslide' requires UI Effects Core).
        /// </summary>
        public string Animated { get; set;}


        /// <summary>
        /// If set, the highest content part is used as height reference for all other parts. Provides more consistent animations.
        /// </summary>
        public bool AutoHeight { get; set; }

   
        /// <summary>
        /// If set, clears height and overflow styles after finishing animations. This enables accordions to work with dynamic content. Won't work together with autoHeight.
        /// </summary>
        public bool ClearStyle { get; set; }

        /// <summary>
        /// Whether all the sections can be closed at once. Allows collapsing the active section by the triggering event (click is the default).
        /// </summary>
         public bool Collapsible{ get; set; }
         
         /// <summary>
         /// The event on which to trigger the accordion.
         /// </summary>
        public string EventTrigger { get; set; }

         /// <summary>
        /// If set, the accordion completely fills the height of the parent element. Overrides autoheight.
         /// </summary>
        public bool FillSpace { get; set; }

        #region Will be implemented on subsequent iteration
        /// <summary>
        /// Icons to use for headers. 
        /// Icons may be specified for 'header' and 'headerSelected',
        /// and we recommend using the icons native to the jQuery UI CSS Framework manipulated by jQuery UI ThemeRoller
        /// </summary>
        public object Icons { get; set; }

        /// <summary>
        /// Selector for the header element.
        /// </summary>
        public object Header { get; set; }

        /// <summary>
        /// If set, looks for the anchor that matches location.href and activates it. Great for href-based state-saving. Use navigationFilter to implement your own matcher.
        /// </summary>
        public bool Navigation { get; set; }

        /// <summary>
        /// Overwrite the default location.href-matching with your own matcher.
        /// </summary>
        public object NavigationFilter { get; set; } 
        #endregion

        #endregion Methods

        #region Overrides


        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            /*
             *  This is the rendered structure by this method           
             * <div id="accordion">
	            <h3><a href="#">Section 1</a></h3>    
	                <div>
             *          content
	                </div>
	             <h3><a href="#">Section 2</a></h3>
	                <div>
             *          content
	                </div>
	            <h3><a href="#">Section 3</a></h3> 
	                <div>
             *          content
	                </div>
	            <h3><a href="#">Section 4</a></h3>
	                <div>
             *             content
	                </div>
            </div> 
             */
            
            //Render Navigation for each Accordion Panel           
             foreach (ContentItem item in this._webAccordionItems)
            {
                    //H3
                    writer.RenderBeginTag(HtmlTextWriterTag.H3);
                    //A     
                    writer.AddAttribute("href", "#");  
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.InnerWriter.Write(item.Name);
                        writer.RenderEndTag(); //End A
                    writer.RenderEndTag();     //End H3
                    //DIV
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    item.Content.WriteTo(writer);// item.Content.Invoke();
                    writer.RenderEndTag(); // End DIV 
         
            }
            //writer.RenderEndTag();  
        }

        protected override void RenderScripts(HtmlTextWriter writer)
        {
           writer.Write(this.Scripts);
        }

        public override void RenderControl(ViewContext context)
        {
            base.RenderControl(context);
        }

        #endregion
    } 
   
 }
